/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pima.gestrest.business;


import com.pima.gestrest.model.Cuisine;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import javax.ejb.Stateless;
import javax.faces.model.SelectItem;
/**
 *
 * @author Pierre-Louis Conte
 */
@Stateless
public class CuisineEJB {

        // ======================================
    // =             Attributes             =
    // ======================================
    @PersistenceContext(unitName = "jsfExamplePU")
    private EntityManager em;

    // ======================================
    // =           Public Methods           =
    // ======================================
    public List<Cuisine> findAll() {
        Query query = em.createNamedQuery(Cuisine.FIND_ALL);
        return query.getResultList();
    }

    public List<SelectItem> getItems()
    {
        ArrayList<SelectItem> items = new ArrayList<SelectItem>();

        Query query = em.createNamedQuery(Cuisine.FIND_ALL);
        for(Cuisine cuisine : (List<Cuisine>)query.getResultList())
        {
            items.add(new SelectItem(cuisine, cuisine.getId()+" - "+cuisine.getTitle()+" - "+cuisine.getRegion() ));
        }
        return items;
    }
    public Cuisine create(Cuisine cuisine) {
        em.persist(cuisine);
        return cuisine;
    }

    public Cuisine update(Cuisine cuisine) {
        return em.merge(cuisine);
    }

    public void delete(List<Cuisine> list) {
        for (Cuisine cuisine : list) {
            delete(cuisine);
        }
    }

    public void delete(Cuisine cuisine) {
        em.remove(em.merge(cuisine));
    }

}
